package com.guit.hrms.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.guit.hrms.dto.interview.InterviewDTO;
import com.guit.hrms.dto.interview.InterviewSituationDTO;
import com.guit.hrms.dto.interview.UpdateInterviewDTO;
import com.guit.hrms.dto.interview.UpdateInterviewStatusDTO;
import com.guit.hrms.event.interview.InterviewCreatedEvent;
import com.guit.hrms.event.interview.InterviewDeletedEvent;
import com.guit.hrms.event.interview.InterviewUpdatedEvent;
import com.guit.hrms.mapper.*;
import com.guit.hrms.po.Employee;
import com.guit.hrms.po.ToEmail;
import com.guit.hrms.po.interview.Interview;
import com.guit.hrms.po.interview.InterviewSituation;
import com.guit.hrms.po.interview.Interviewee;
import com.guit.hrms.po.utilsPO.PageBean;
import com.guit.hrms.service.InterviewService;
import com.guit.hrms.utils.CalculateTheData;
import com.guit.hrms.utils.EmailUtil;
import com.guit.hrms.utils.ThreadLocalUtil;
import com.guit.hrms.vo.recruitmentVO.PlanDeptJobVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

@Service
@Slf4j
public class InterviewServiceImpl implements InterviewService {
    @Autowired
    private InterviewMapper interviewMapper;
    @Autowired
    private IntervieweeMapper intervieweeMapper;
    @Autowired
    private RecruitmentMapper recruitmentMapper;
    @Autowired
    private OnboardingMapper onboardingMapper;
    @Autowired
    private QueryMapper queryMapper;
    @Autowired
    private EmailUtil emailUtil;
    @Autowired
    private CalculateTheData calculateTheData;
    @Value("${spring.mail.username1}")
    private String username1;
    @Autowired
    private ApplicationEventPublisher eventPublisher;

    /**
     * 添加面试安排
     * @param interviewDTO 面试安排信息
     * @return Result
     */
    @Override
    public Integer addInterview(InterviewDTO interviewDTO) {
        String[] interviewers = interviewDTO.getInterviewerIds().split(",");
        Employee employee = null;
        for (String interviewer : interviewers) {
            employee = queryMapper.getById(interviewer);
            if (employee == null) {
                return 0;
            }
        }
        Integer type = recruitmentMapper.getPlanTypeByPlanId(interviewDTO.getPlanId());
        if (type == null){
            return 0;
        }
        Interview interview = new Interview();
        BeanUtils.copyProperties(interviewDTO,interview);
        Map<String,Object> map = ThreadLocalUtil.get();
        String employeeId = map.get("employeeId").toString();
        interview.setCreateTime(LocalDateTime.now());
        interview.setUpdateTime(LocalDateTime.now());
        interview.setCreateBy(employeeId);
        interview.setUpdateBy(employeeId);
        PlanDeptJobVO planDeptJobVO = null;
        Integer row = 0;
        Integer id = 0;
        if (type==1){
            planDeptJobVO = recruitmentMapper.getPlanDeptJob(interviewDTO.getPlanId(), interviewDTO.getDeptId(), interviewDTO.getPositionNumber());
        } else if (type==2){
            planDeptJobVO = recruitmentMapper.getPlanAcademyJob(interviewDTO.getPlanId(), interviewDTO.getDeptId(), interviewDTO.getPositionNumber());
        } else {
            row = interviewMapper.selectIsRepeatPlaceAndTime(interviewDTO);
            if (row>0){
                return 0;
            }
            id = interviewMapper.addInterview(interview);
            if (id != 0){
                interview.setId(id);
                eventPublisher.publishEvent(new InterviewCreatedEvent(this, interview));
                return 1;
            } else {
                return 0;
            }

        }
        if (planDeptJobVO!=null){
            row = interviewMapper.selectIsRepeatPlaceAndTime(interviewDTO);
            if (row>0){
                return 0;
            }
            id = interviewMapper.addInterview(interview);
            if (id != 0){
                interview.setId(id);
                eventPublisher.publishEvent(new InterviewCreatedEvent(this, interview));
                return 1;
            } else {
                return 0;
            }
        } else {
            return 0;
        }
    }

    /**
     * 获取个人面试安排
     * @param page 页码
     * @param pageSize 页大小
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @param employeeId 员工号
     * @return Result
     */
    @Override
    @Transactional
    public PageBean getPersonalInterviews(Integer page, Integer pageSize, LocalDateTime startTime, LocalDateTime endTime, String employeeId) {
        PageHelper.startPage(page, pageSize);
        List<Interview> interviews = interviewMapper.getPersonalInterviews(startTime, endTime, employeeId);
        for (Interview interview : interviews){
            String[] interviewers = interview.getInterviewerIds().split(",");
            Employee employee = null;
            for (String interviewer : interviewers) {
                employee = queryMapper.getById(interviewer);
                if (employee == null) {
                    throw new RuntimeException("面试官不存在");
                } else {
                    interview.setInterviewerNames(interview.getInterviewerNames() + employee.getUsername() + "  ");
                }
            }
            String creatorName = queryMapper.getById(interview.getCreateBy()).getUsername();
            if (creatorName == null) {
                throw new RuntimeException("创建人不存在");
            } else {
                interview.setCreatorName(creatorName);
            }
            String updaterName = queryMapper.getById(interview.getUpdateBy()).getUsername();
            if (updaterName == null) {
                throw new RuntimeException("更新人不存在");
            } else {
                interview.setUpdaterName(updaterName);
            }
            interview.setIntervieweeMsg(interviewMapper.getIntervieweeMsg(interview.getId()));
        }
        Page<Interview> p = (Page<Interview>) interviews;
        return new PageBean(p.getTotal(), p.getResult());
    }

    /**
     * 获取所有面试安排
     * @param page 页码
     * @param pageSize 页大小
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @return Result
     */
    @Override
    public PageBean getAllInterviews(Integer page, Integer pageSize, LocalDateTime startTime, LocalDateTime endTime) {
        PageHelper.startPage(page, pageSize);
        List<Interview> interviews = interviewMapper.getPersonalInterviews(startTime, endTime, null);
        for (Interview interview : interviews){
            String[] interviewers = interview.getInterviewerIds().split(",");
            Employee employee = null;
            for (String interviewer : interviewers) {
                employee = queryMapper.getById(interviewer);
                if (employee == null) {
                    throw new RuntimeException("面试官不存在");
                } else {
                    interview.setInterviewerNames(interview.getInterviewerNames() + employee.getUsername() + "  ");
                }
            }
            String creatorName = queryMapper.getById(interview.getCreateBy()).getUsername();
            if (creatorName == null) {
                throw new RuntimeException("创建人不存在");
            } else {
                interview.setCreatorName(creatorName);
            }
            String updaterName = queryMapper.getById(interview.getUpdateBy()).getUsername();
            if (updaterName == null) {
                throw new RuntimeException("更新人不存在");
            } else {
                interview.setUpdaterName(updaterName);
            }
            interview.setIntervieweeMsg(interviewMapper.getIntervieweeMsg(interview.getId()));
        }
        Page<Interview> p = (Page<Interview>) interviews;
        return new PageBean(p.getTotal(), p.getResult());
    }

    /**
     * 修改面试安排
     * @param updateInterviewDTO 面试安排信息
     * @return Result
     */
    @Override
    @Transactional
    public Integer updateInterview(UpdateInterviewDTO updateInterviewDTO) {
        if (updateInterviewDTO.getStartTime() != null && updateInterviewDTO.getEndTime() != null && updateInterviewDTO.getStartTime().isAfter(updateInterviewDTO.getEndTime())){
            return 0;
        }
        if (updateInterviewDTO.getInterviewerIds() != null){
            String[] interviewers = updateInterviewDTO.getInterviewerIds().split(",");
            Employee employee = null;
            for (String interviewer : interviewers) {
                employee = queryMapper.getById(interviewer);
                if (employee == null) {
                    return 0;
                }
            }
        }

        Interview interview = new Interview();
        BeanUtils.copyProperties(updateInterviewDTO,interview);
        interview.setId(updateInterviewDTO.getInterviewId());
        Map<String,Object> map = ThreadLocalUtil.get();
        String employeeId = map.get("employeeId").toString();
        interview.setUpdateTime(LocalDateTime.now());
        interview.setUpdateBy(employeeId);
        Interview interview1 = interviewMapper.getInterviewById(updateInterviewDTO.getInterviewId());
        if (updateInterviewDTO.getPlace() != null || updateInterviewDTO.getStartTime() != null || updateInterviewDTO.getEndTime() != null){
            InterviewDTO interviewDTO = new InterviewDTO();
            if (interview1 == null){
                return 0;
            }
            if (updateInterviewDTO.getPlace() == null){
                interviewDTO.setPlace(interview1.getPlace());
            }
            if (updateInterviewDTO.getStartTime() == null){
                interviewDTO.setStartTime(interview1.getStartTime());
            }
            if (updateInterviewDTO.getEndTime() == null){
                interviewDTO.setEndTime(interview1.getEndTime());
            }
            BeanUtils.copyProperties(updateInterviewDTO,interviewDTO);
            Integer row = interviewMapper.selectIsRepeatPlaceAndTime1(interviewDTO, updateInterviewDTO.getInterviewId());
            if (row>0){
                return 0;
            }
        }

        Integer result =  interviewMapper.updateInterview(interview);
        if (result == 0){
            return 0;
        } else {
            BeanUtils.copyProperties(interview,interview1);
            eventPublisher.publishEvent(new InterviewUpdatedEvent(this, interview1));
            if (updateInterviewDTO.getPlace() != null || updateInterviewDTO.getStartTime() != null || updateInterviewDTO.getEndTime() != null){
                List<Interviewee> interviewees = interviewMapper.getIntervieweeMsg(updateInterviewDTO.getInterviewId());
                List<String> emails = new ArrayList<>();
                for (Interviewee interviewee : interviewees){
                    emails.add(interviewee.getEmail());
                }
                ToEmail toEmail = new ToEmail();
                toEmail.setTos(emails.toArray(new String[0]));
                toEmail.setSubject("面试安排改动通知");
                toEmail.setContent("您面试安排已发生变动，请及时查看");
                if (updateInterviewDTO.getPlace() != null){
                    toEmail.setContent(toEmail.getContent() + "，面试地点改为：" + updateInterviewDTO.getPlace());
                }
                if (updateInterviewDTO.getStartTime() != null){
                    toEmail.setContent(toEmail.getContent() + "，面试开始时间改为：" + updateInterviewDTO.getStartTime());
                }
                if (updateInterviewDTO.getEndTime() != null){
                    toEmail.setContent(toEmail.getContent() + "，面试结束时间改为：" + updateInterviewDTO.getEndTime());
                }
                emailUtil.sendSimpleEmail(toEmail);
            }
            return 1;
        }
    }

    /**
     * 删除面试安排
     * @param interviewIds 面试安排id
     * @return Result
     */
    @Override
    @Transactional
    public Integer deleteInterview(List<Integer> interviewIds) {
        Interview interview = null;
        List<Interview> interviews = new ArrayList<>();
        Integer r1 = 0;
        Integer r2 = 0;
        Integer r3 = 0;
        List<Interviewee> interviewees;
        List<String> emails;
        List<ToEmail> toEmails = new ArrayList<>();
        ToEmail toEmail;
        UpdateInterviewStatusDTO updateInterviewStatusDTO = new UpdateInterviewStatusDTO();
        updateInterviewStatusDTO.setInterviewStatus(0);
        for (Integer interviewId : interviewIds){
            interview = interviewMapper.getInterviewById(interviewId);
            if (interview == null){
                return 0;
            }
            interviewees = interviewMapper.getIntervieweeMsg(interviewId);
            r1 = interviewMapper.deleteInterviewSituationByInterviewId(interviewId);
            r2 = interviewMapper.deleteInterviewById(interviewId);
            if (r2 == 0){
                throw new RuntimeException("删除出错！");
            }
            if (interview.getEndTime().isBefore(LocalDateTime.now())){
                continue;
            }
            emails = new ArrayList<>();
            for (Interviewee interviewee : interviewees){
                updateInterviewStatusDTO.setIntervieweeIds(new ArrayList<>());
                updateInterviewStatusDTO.getIntervieweeIds().add(interviewee.getId());
                r3 = intervieweeMapper.updateInterviewStatus(updateInterviewStatusDTO);
                if (r3 == 0){
                    throw new RuntimeException("修改应聘者面试状态出错！");
                }
                emails.add(interviewee.getEmail());
            }
            eventPublisher.publishEvent(new InterviewDeletedEvent(this, interview));
            toEmail = new ToEmail();
            toEmail.setTos(emails.toArray(new String[0]));
            toEmail.setSubject("面试取消通知");
            toEmail.setContent("您"+interview.getStartTime()+"至"+interview.getEndTime()+"在"+interview.getPlace()+"的面试安排已取消，请您注意！");
            toEmails.add(toEmail);
        }
        for (ToEmail toEmail1 : toEmails){
            emailUtil.sendSimpleEmail(toEmail1);
        }
        return 1;
    }

    /**
     * 获取面试详情
     * @param page 页码
     * @param pageSize 页大小
     * @param interviewId 面试id
     * @param intervieweeId 应聘者id
     * @return Result
     */
    @Override
    public PageBean getInterviewSituation(Integer page, Integer pageSize, Integer interviewId, Integer intervieweeId) {
        PageHelper.startPage(page, pageSize);
        List<InterviewSituation> interviewSituations = interviewMapper.getInterviewSituations(interviewId, intervieweeId);
        for (InterviewSituation interviewSituation : interviewSituations){
            interviewSituation.setName(interviewMapper.getIntervieweeById(interviewSituation.getIntervieweeId()).getName());
        }
        Page<InterviewSituation> p = (Page<InterviewSituation>) interviewSituations;
        return new PageBean(p.getTotal(), p.getResult());
    }

    /**
     * 更新面试详情（为应聘者评分）
     * @param interviewSituationDTO 面试详情信息
     * @return Result
     */
    @Override
    @Transactional
    public Integer updateInterviewSituation(InterviewSituationDTO interviewSituationDTO) {
        try {
            // 1. 参数校验
            if (interviewSituationDTO.getProfessionalSkills() < 1 || interviewSituationDTO.getProfessionalSkills() > 100 ||
                interviewSituationDTO.getCommunicationSkills() < 1 || interviewSituationDTO.getCommunicationSkills() > 100 ||
                interviewSituationDTO.getTeamworkAbility() < 1 || interviewSituationDTO.getTeamworkAbility() > 100) {
                throw new RuntimeException("评分必须在1-100分之间");
            }

            // 2. 获取原面试记录
            InterviewSituation interviewSituation = new InterviewSituation();
            BeanUtils.copyProperties(interviewSituationDTO, interviewSituation);
            
            InterviewSituation existing = interviewMapper.getInterviewSituation(interviewSituation.getId());
            if (existing == null) {
                throw new RuntimeException("面试记录不存在");
            }
            
            // 3. 设置必要字段
            interviewSituation.setUpdateTime(LocalDateTime.now());
            Map<String,Object> map = ThreadLocalUtil.get();
            String employeeId = map.get("employeeId").toString();
            interviewSituation.setEvaluatorId(employeeId);
            interviewSituation.setIntervieweeId(existing.getIntervieweeId());
            interviewSituation.setInterviewId(existing.getInterviewId());

            // 4. 计算综合评分
//            int overallRating = (interviewSituation.getProfessionalSkills() +
//                               interviewSituation.getCommunicationSkills() +
//                               interviewSituation.getTeamworkAbility()) / 3;
            int overallRating =  calculateTheData.weightedAverage(interviewSituation);
            interviewSituation.setOverallRating(overallRating);

            // 5. 更新面试情况
            int result = interviewMapper.updateInterviewSituation(interviewSituation);
            if (result == 0) {
                throw new RuntimeException("更新面试评分失败");
            }

            // 6. 如果面试通过，更新相关状态
            if (interviewSituation.getResult() == 1) {
//                // 更新应聘者状态
//                UpdateInterviewStatusDTO updateDTO = new UpdateInterviewStatusDTO();
//                updateDTO.setIntervieweeIds(new ArrayList<>());
//                updateDTO.getIntervieweeIds().add(interviewSituation.getIntervieweeId());
//                updateDTO.setInterviewStatus(2);
//
//                int statusResult = intervieweeMapper.updateInterviewStatus(updateDTO);
//                if (statusResult == 0) {
//                    throw new RuntimeException("更新应聘者状态失败");
//                }
                
                // 更新通过人数
                int throughResult = interviewMapper.updateThroughNumber(interviewSituation.getInterviewId(), 1);
                if (throughResult == 0) {
                    throw new RuntimeException("更新面试通过人数失败");
                }
            }

            // 7. 发送邮件通知
            Interviewee interviewee = interviewMapper.getIntervieweeById(existing.getIntervieweeId());
            if (interviewee != null) {
                ToEmail toEmail = new ToEmail();
                toEmail.setTos(new String[]{interviewee.getEmail()});
                toEmail.setSubject("面试结果通知");
                toEmail.setContent("您的面试结果已出，请您注意查收！\n" + 
                                 (interviewSituation.getResult() == 1 ? 
                                 "恭喜您，本次面试通过！" : 
                                 "很遗憾，本次面试未通过！"));
                emailUtil.sendSimpleEmail(toEmail);
            }

            return 1;
        } catch (Exception e) {
            throw new RuntimeException("更新面试情况失败: " + e.getMessage());
        }
    }

    /**
     * 开始面试
     * @param interviewId 面试安排id
     * @param startOrEnd 1表示开始，2表示结束
     * @return Result
     */
    @Override
    @Transactional
    public Integer interviewStartOrEnd(Integer interviewId, Integer startOrEnd) {
        List<Interviewee> interviewees = interviewMapper.getIntervieweeMsg(interviewId);
        if (interviewees.isEmpty()){
            throw new RuntimeException("该面试安排没有应聘者！");
        } else {
            UpdateInterviewStatusDTO updateInterviewStatusDTO = new UpdateInterviewStatusDTO();
            if (startOrEnd == 1){
                updateInterviewStatusDTO.setInterviewStatus(2);
            } else if (startOrEnd == 2){
                updateInterviewStatusDTO.setInterviewStatus(0);
            }
            updateInterviewStatusDTO.setIntervieweeIds(new ArrayList<>());
            for (Interviewee interviewee : interviewees){
                updateInterviewStatusDTO.getIntervieweeIds().add(interviewee.getId());
            }
            Integer result = intervieweeMapper.updateInterviewStatus(updateInterviewStatusDTO);
            if (result == 0){
                throw new RuntimeException("更新应聘者状态失败！");
            } else {
                return 1;
            }
        }
    }

    /**
     * 查询所有没开始的面试
     * @param page 当前页
     * @param pageSize 每页大小
     * @return PageBean
     */
    @Override
    public PageBean findAllActiveInterviews(int page, int pageSize) {
        PageHelper.startPage(page, pageSize);
        List<Interview> interviews = interviewMapper.findAllActiveInterviews();
        Page<Interview> interviewPage = (Page<Interview>) interviews;
        return new PageBean(interviewPage.getTotal(), interviewPage.getResult());
    }

    /**
     * 为与即将开始面试相关的面试官和应聘者发送邮件
     * @param id 面试安排id
     */
    @Override
    public void sendInterviewEmail(Integer id) {
        Interview interview = interviewMapper.getInterviewById(id);
        List<String> errorMsg = new ArrayList<>();
        boolean hasError = false;
        List<String> emails = new ArrayList<>();
        ToEmail toEmail = new ToEmail();
        if (interview == null) {
            errorMsg.add("查询不到id为" + id + "的面试安排");
            emails.add(username1);
            toEmail.setTos(emails.toArray(new String[0]));
            toEmail.setSubject("查询面试安排失败");
            log.error("查询不到id为{}的面试安排", id);
            toEmail.setContent(errorMsg.toString());
            emailUtil.sendSimpleEmail(toEmail);
            throw new RuntimeException(String.valueOf(errorMsg));
        }
        String[] interviewers = interview.getInterviewerIds().split(",");
        Employee employee = null;
        for (String interviewer : interviewers) {
            employee = queryMapper.getById(interviewer);
            if (employee == null) {
                hasError = true;
                errorMsg.add("查询不到id为" + interviewer + "的面试官");
                log.error("查询不到id为{}的面试官", interviewer);
            } else {
                emails.add(employee.getEmail());
            }
        }
        List<Interviewee> interviewees = interviewMapper.getIntervieweeMsg(id);
        if (!interviewees.isEmpty()){
            emails.addAll(interviewees.stream()
                    .map(Interviewee::getEmail)
                    .toList());
        }
        toEmail.setTos(emails.toArray(new String[0]));
        toEmail.setSubject("面试即将开始通知");
        toEmail.setContent("您"+interview.getStartTime()+"至"+interview.getEndTime()+"在"+interview.getPlace()+"的面试还有不到15分钟开始，请您及时做好准备！");
        emailUtil.sendSimpleEmail(toEmail);
        if (hasError){
            emails = Collections.singletonList(username1);
            toEmail.setTos(emails.toArray(new String[0]));
            toEmail.setSubject("查询面试安排失败");
            toEmail.setContent(errorMsg.toString());
            emailUtil.sendSimpleEmail(toEmail);
            throw new RuntimeException(String.valueOf(errorMsg));
        }
    }

//    /**
//     * 查询超时offer
//     */
//    @Override
//    @Transactional
//    public void checkExpiredOffersBatch() {
//        int pageSize = 100;
//        int offset = 0;
//        Integer result = 0;
//        boolean hasMore = true;
//
//        while (hasMore) {
//            List<Offer> offers = interviewMapper.checkExpiredOffersBatch(LocalDateTime.now(),pageSize, offset);
//            List<Integer> ids = offers.stream()
//                    .map(Offer::getId)
//                    .toList();
//            if (ids.isEmpty()) {
//                hasMore = false;
//            } else {
//                List<Interviewee> interviewees = new ArrayList<>();
//                Interviewee interviewee = null;
//                List<String> errorMsg = new ArrayList<>();
//                boolean hasError = false;
//                // 批量更新状态
//                result = interviewMapper.updateOfferResult(ids, 2);
//
//                for (Integer intervieweeId : ids) {
//                    interviewee = intervieweeMapper.getIntervieweeById(intervieweeId);
//                    if (interviewee == null) {
//                        hasError = true;
//                        errorMsg.add("offer超时处理时，应聘者ID为" + intervieweeId + "的应聘者不存在\n");
//                        log.error("offer超时处理时，应聘者ID为 {} 的应聘者不存在", intervieweeId);
////                        throw new RuntimeException("该应聘者不存在");
//                    }
//                    UpdateInterviewStatusDTO updateInterviewStatusDTO = new UpdateInterviewStatusDTO();
//                    List<Integer> intervieweeIds = new ArrayList<>();
//                    intervieweeIds.add(intervieweeId);
//                    updateInterviewStatusDTO.setIntervieweeIds(intervieweeIds);
//                    updateInterviewStatusDTO.setInterviewStatus(7);
//                    Integer r = intervieweeMapper.updateInterviewStatus(updateInterviewStatusDTO);
//                    if (r == 0) {
//                        hasError = true;
//                        errorMsg.add("offer超时处理时，应聘者ID为" + intervieweeId + "的应聘者面试状态更新失败\n");
//                        log.error("offer超时处理时，应聘者ID为 {} 的应聘者面试状态更新失败", intervieweeId);
////                        throw new RuntimeException("修改应聘者面试状态失败");
//                    }
//                    r = onboardingMapper.deleteOnboardingByIntervieweeId(intervieweeId);
//                    if (r == 0) {
//                        hasError = true;
//                        errorMsg.add("offer超时处理时，应聘者ID为" + intervieweeId + "的待入职信息删除失败\n");
//                        log.error("offer超时处理时，应聘者ID为 {} 的待入职信息删除失败", intervieweeId);
////                        throw new RuntimeException("删除待入职信息失败");
//                    }
//                    interviewees.add(interviewee);
//                }
//
//                ToEmail toEmail = new ToEmail();
//                List<String> emails = new ArrayList<>();
//                if (result == 0 || hasError){
//                    emails.add(username1);
//                    toEmail.setTos(emails.toArray(new String[0]));
//                    toEmail.setSubject("offer超时修改状态失败");
//                    if (result == 0){
//                        toEmail.setContent("offer超时修改状态失败的ids: "+ ids + "\n");
//                        log.error("offer超时修改状态失败的ids： {}", ids);
//                    }
//                    if (hasError){
//                        toEmail.setContent(toEmail.getContent() != null ? toEmail.getContent()+"offer超时处理时出现错误，错误信息: "+ errorMsg : "offer超时处理时出现错误，错误信息: "+ errorMsg);
//                    }
//                    emailUtil.sendSimpleEmail(toEmail);
//                } else {
////                    interviewee = null;
////                    for (Offer offer : offers){
////                        interviewee = interviewMapper.getIntervieweeById(offer.getIntervieweeId());
////                        if (interviewee != null){
////                            emails.add(interviewee.getEmail());
////                        }
////                    }
//                    emails = interviewees.stream()
//                            .map(Interviewee::getEmail)
//                            .collect(Collectors.toList());
//                    toEmail.setTos(emails.toArray(new String[0]));
//                    toEmail.setSubject("offer接受过期通知");
//                    toEmail.setContent("您未在规定时间内回复是否接受offer，现以视您放弃！");
//                    emailUtil.sendSimpleEmail(toEmail);
//                }
//                offset += pageSize;
//            }
//        }
//    }
}
